package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.alibaba.fastjson.JSONArray;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.csv.CsvExporter;
import com.pi.framework.web.util.CsvView;
import com.pi.pro.xcmg.am.was.fac.service.InforCommonWorkService;
import com.pi.wms.entity.TwiInventoryXwmsDetails;
import com.pi.wms.entity.TwtActDailyWorkSummary;
import com.pi.wms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
@RequestMapping("/report/dailyworksummreport")
public class DailyWorksummReportController extends BaseCrudController<TwtActDailyWorkSummary, Long> {

	@Autowired
	private DailyWorkSummService service;

	@Override
	protected ICrudService<TwtActDailyWorkSummary, Long> getCrudService() {
		return service;
	}

	@Override
	protected String getPrefix() {
		return "dailyworksummreport";
	}

	@Override
	protected TwtActDailyWorkSummary newModel(HttpServletRequest request) {
		return new TwtActDailyWorkSummary();
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		super.prepareDatatableConditions(query, input);
	}

	@PostMapping(value = { "/export" })
	public ModelAndView export(@RequestParam Map<String, Object> query, DataTablesInput input) {
		String inputParams = "";
		if (query.get("input")!=null){
			inputParams=query.get("input").toString();
		}
		if (StringUtils.isNotEmpty(inputParams)) {
			input = JSONArray.parseObject(inputParams, DataTablesInput.class);
		}
		input.setLength(-1);
		String ymd = DateUtils.format(new Date(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS);
		final DataTablesInput ins = input;
		CsvExporter<TwtActDailyWorkSummary> exporter = new CsvExporter<TwtActDailyWorkSummary>() {
			@Override
			public String[] header() {
				return "物料编码,物料名称,物料WBS,库存数量,单位,供应商,货权方,SAP库位,WTwtActDailyWorkSummaryMS库位,WMS批,收货日期,查询时间"
						.split(",");
			}

			@Override
			public Collection<TwtActDailyWorkSummary> nextList(int pageSize, int page) {
				ins.setLength(pageSize);
				ins.setStart(page * pageSize);
				Pageable pageable = PageRequest.of(page, pageSize);
				Page<TwtActDailyWorkSummary> out = service.findAll(query,pageable);
				return out == null ? null : out.getContent();
			}

			@Override
			public Collection<String> toText(TwtActDailyWorkSummary d) {
				List<String> item = new ArrayList<String>();
				String recvtime="";
				if (recvtime.length()>10){
					recvtime=recvtime.substring(0, 10);
				}
				item.add(recvtime);
				//item.add(DateUtils.format(d.getXwmsReceiveTime(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
				item.add(DateUtils.format(d.getCreatedDate(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
				return item;
			}
		};
		return new ModelAndView(new CsvView(exporter, "待入库清单_" + ymd));
	}


}
